package de.gameduell.framework.threads 
{
	import de.gameduell.framework.queue.*;
	import flash.events.Event;
	/**
	 * A BlockingActionThread wrap an action as a thread to execute actions in different action/thread queues.
	 * @author Ferry Trunschke
	 */
	public class BlockingActionThread extends ThreadAdapter {
		
		// wrapped action
		public var action:IGDAction;
		public var finished:Boolean = false;
		
		public function BlockingActionThread(action:IGDAction) {
			this.action = action;
		}
		
		// allow to register event, just delegate to onFinishExecution()
		private function onActionDone(event:Event):void {
			onFinishExecution();
		}
		
		/* INTERFACE de.gameduell.framework.threads.IBlockingThread */

		public override function run():Boolean {
			return !finished;
		}
		
		public override function onStartExecution():void {
			action.addEventListener(ActionEvent.DONE, onActionDone);		
			action.compute();				
		}
		
		public override function onFinishExecution():void {
			action.removeEventListener(ActionEvent.DONE, onActionDone);	
			finished = true;
		}
		
		public override function get isBlockingThread():Boolean {
			return true;
		}		
		
		public override function get description():String {
			return "BlockingActionThread: " + action.description + " finished=" + finished;
		}
		
	}

}